package com.bstek.uflo.test;

import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.model.task.TaskState;
import com.bstek.uflo.query.TaskQuery;
import com.bstek.uflo.service.ProcessService;
import com.bstek.uflo.service.StartProcessInfo;
import com.bstek.uflo.service.TaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description ulo演示demo
 * @Author hans
 * @CreateDate 2020-9-19
 */
@Service
public class UfloDemo {

    @Resource(name = ProcessService.BEAN_ID)
    private ProcessService processService;

    @Resource(name = TaskService.BEAN_ID)
    private TaskService taskService;

    /**
     * 部署流程图
     *
     * @param filePath
     * @throws FileNotFoundException
     */
    public void deploy(String filePath) throws FileNotFoundException {
        File f = new File(filePath);
        InputStream inputStream = new FileInputStream(f);
        processService.deployProcess(inputStream);
    }

    /**
     * 开启一个流程实例
     * http://wiki.bsdn.org/pages/viewpage.action?pageId=32440459
     *
     * @param data
     */
    public void setProcess(Map<String, Object> data) {
        StartProcessInfo info = new StartProcessInfo("admin");
        info.setBusinessId("1");
        processService.startProcessById(101, info);
    }

    /**
     * 处理任务
     *
     * @param taskId
     */
    public void completeTask(long taskId) {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("businessName", "我是测试啊");
        variables.put("businessOwner", "admin");
        taskService.start(taskId);
        taskService.complete(taskId, variables);
    }

    /**
     * 查询指定用户的待办任务
     *
     * @param userName
     * @return
     */
    public List<Task> loadTask(String userName) {
        TaskQuery query = taskService.createTaskQuery();
        query.addTaskState(TaskState.Created);
        query.addTaskState(TaskState.InProgress);
        query.addTaskState(TaskState.Ready);
        query.addTaskState(TaskState.Suspended);
        query.addTaskState(TaskState.Reserved);
        query.addAssignee(userName).addOrderDesc("createDate");
        return query.list();
    }

}
